package com.horoscope.astrology.zodiac.signs.free.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.ContentInfo
import com.horoscope.astrology.zodiac.signs.free.utils.HtmlToText
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils

class RelationAdapter(private val mContext: Context) : BaseAdapter() {
    private val mData = ArrayList<ContentInfo>()

    fun setData(list: ArrayList<ContentInfo>) {
        mData.clear()
        mData.addAll(list)
        notifyDataSetChanged()

    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_chart_content, parent, false)
        return ContentViewHolder(view)
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    private var targetS = ""
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is ContentViewHolder -> {
                val bean = mData[position]
                if (bean.content.length > 500 && needAD(bean.title)) {
                    targetS = bean.content.substring(0, 500)
                    holder.mask.visibility = View.VISIBLE
                } else if (bean.content.isEmpty()) {
                    holder.content.visibility = View.GONE
                    holder.mask.visibility = View.GONE
                    holder.video.visibility = View.GONE
                    holder.root.setPadding(0, 0, 0, SysUtils.rp(10f))
                } else {
                    targetS = bean.content
                    holder.mask.visibility = View.INVISIBLE
                }
                holder.title.text = bean.title
                holder.content.text = HtmlToText.delHTMLTag(targetS)

                holder.video.setOnClickListener {
                    if (!SysUtils.isFastClick()) mOnItemClickedListener!!.onItemClick(
                        it,
                        position,
                        ""
                    )
                }
            }
        }
    }

    fun notifyPositionData(position: Int) {
        AppSettingManager.saveBoolean(false, mData[position].title)
        notifyItemChanged(position)
    }

    private fun needAD(key: String): Boolean {
        return AppSettingManager.readBoolean(key, true)
    }

    private inner class ContentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val root: LinearLayout = itemView.findViewById(R.id.ll_root)
        val title = itemView.findViewById<TextView>(R.id.text_View_title)
        val content = itemView.findViewById<TextView>(R.id.text_view_content)
        val mask = itemView.findViewById<FrameLayout>(R.id.frame_mask)
        val video = itemView.findViewById<LinearLayout>(R.id.linear_watch_video)
    }
}